﻿using System;
using System.Globalization;
using EPiServer.Web.WebControls;
using OpenWaves.ImageTransformations.Web.Controls;

namespace OpenWaves.EPiServer.ImageProperty
{
    public class ImageProperty : Property
    {
        public const string UIHint = "ow.image";

        public ImageTransformationType Transformation
        {
            get
            {
                ImageTransformationType imageTransformation;
                if (Enum.TryParse(this.RenderSettings.GetAttribute("ImageTransformation"), true,
                                                     out imageTransformation))
                {
                    return imageTransformation;
                }
                return ImageTransformationType.None;
            }
            set
            {
                this.RenderSettings.SetAttribute("ImageTransformation", value.ToString());
            }
        }

        public int ImageWidth
        {
            get
            {
                return this.GetIntegerValue("ImageWidth");
            }
            set
            {
                this.RenderSettings.SetAttribute("ImageWidth", value.ToString(CultureInfo.InvariantCulture));
            }
        }

        public int ImageHeight
        {
            get
            {
                return this.GetIntegerValue("ImageHeight");
            }
            set
            {
                this.RenderSettings.SetAttribute("ImageHeight", value.ToString(CultureInfo.InvariantCulture));
            }
        }

        public string AlternateText
        {
            get
            {
                return this.RenderSettings.GetAttribute("AlternateText");
            }
            set
            {
                this.RenderSettings.SetAttribute("AlternateText", value);
            }
        }

        private int GetIntegerValue(string keyName)
        {
            int value;
            if (int.TryParse(this.RenderSettings.GetAttribute(keyName), NumberStyles.Integer,
                             CultureInfo.InvariantCulture, out value))
            {
                return value;
            }
            return 0;
        }
    }
}